<template>

	<view class="main-box">
	

	</view>
</template>

<script>
		import FingerprintJS from '@fingerprintjs/fingerprintjs' // 引入
	import {
		getip,
		getUrl,
		subOrder,
		getPhonetype,
			logs
	} from "../../api/index.js"
	export default {
		data() {
			return {
				flag: false,
				mobile: "",
				show: true,
				a: '',
				param: "",
				orderNo: "", //订单号
				adst: 0, //信封显示与否
				url: "",
				chinfo:'',
				deviceid:''
			};
		},
		onShow() {
	//每次进入页面刷新记录
			this.getvisitorId()
		},
		onLoad(option) {
			if(option.chinfo){
				this.chinfo=option.chinfo
			}
		

			if (option.urlCode) {
				this.param = option.urlCode
				uni.setStorageSync('param', option.urlCode)
			}

			if (option.tokenId) {
				this.param = option.tokenId
				uni.setStorageSync('param', option.tokenId)
			}
			if (option.clickid) {
				uni.setStorageSync('param', option.clickid)
				this.param = option.clickid
			}
			if (option.a_oId) {
				uni.setStorageSync('param', option.a_oId)
				this.param = option.a_oId
			}
			if (option.a_oid) {
				uni.setStorageSync('param', option.a_oid)
				this.param = option.a_oid
			}
			if (option.param) {
				uni.setStorageSync('param', option.param)
				this.param = option.param
			}
			console.log(this.param)
			if (option.a) {
				uni.setStorageSync('a', option.a)
				this.a = option.a
				const formDatax = new FormData();

				formDatax.append('a', uni.getStorageSync('a'));
				fetch('https://www.sx2b2c.com/api/dhk3000/getAdUrl', {
						method: 'post',
						body: formDatax
					})
					.then(response => response.json())
					.then(data => {
						console.log(data)
						this.adst = data.data.adst
						this.url = data.data.url1 //信封跳转地址


					})
					.catch(error => console.error(error));

			}
			
			
		},

		methods: {
			async getvisitorId() {
				const fp = await FingerprintJS.load()
				const result = await fp.get()
				this.deviceid = result.visitorId
				logs({
					a: this.a,
					eventType: 1,
					xcx: 2,
					deviceid: this.deviceid
				}).then((res) => {
					console.log(res)
					this.clcpic()
				})
				console.log(this.deviceid)
			},
			changes() {
				// 在这里编写处理点击事件的逻辑
				window.location.href = this.url
			},
	
			//获取订单号
			getRandomNum() {
				var a = new Date().getFullYear() + (new Date().getMonth() + 1).toString().padStart(2, "0") +
					new Date().getDate().toString().padStart(2, "0")
				//  "2020-02-12"   获取的是当前日期
				var b = new Date().getHours().toString().padStart(2, "0") + new Date().getMinutes().toString()
					.padStart(2, "0") + new Date().getSeconds().toString().padStart(2, "0")
				//11:13:02当前时间
				var randomNum = "" + Math.round(Math.random() * 900000000);
				while (randomNum.length < 8) {
					randomNum = "0" + randomNum;
				}
				const number = 'AD' + a + b + randomNum
				this.orderNo = number


				return randomNum;
			},


			//点击图片
			clcpic() {
				//每次进入页面刷新记录
				logs({a:this.a,eventType:2,xcx:2,deviceid: this.deviceid}).then((res)=>{
					console.log(res)
				})
				this.getRandomNum() //调用生成订单号
				getip().then((res) => {
					console.log(res.data.city)
					if (res.data.city == "深圳") {
						getUrl({
							a: this.a,
							param: this.param,
							mobile: this.mobile
						}).then((result) => {
							console.log(result.data.ad_h5_to_url)
							if (result.code == 1 && result.data.ad_h5_to_url != '') {
								window.location.href = result.data.ad_h5_to_url
							} else {
								uni.showToast({
									icon: 'none',
									title: '抱歉，当前暂无可领取福利',
									duration: 2000
								});
							}
						})

					} else {
						// #ifdef H5
						uni.setStorageSync('phones', this.mobile)
						if (/MicroMessenger/.test(window.navigator.userAgent)) {
							let system = navigator.userAgent.toLowerCase();
							//微信环境==苹果跳转支付宝
								subOrder({
									a: this.a,
									param: this.param,
									mobile: this.mobile,
									nomobile: '1',
									from_t: '1',
									from_env: '2',
									orderNo: this.orderNo
								}).then((res) =>{
						let link = "https://render.alipay.com/p/s/i/?scheme=" +
							encodeURIComponent(
								"alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode="
							) +
							encodeURIComponent(encodeURIComponent('https://render.alipay.com/p/s/i/?scheme=' + encodeURIComponent(
								'alipays://platformapi/startapp?appId=2021004123656452&page=' + encodeURIComponent(
									'pages/newbanner/newbanner?__id__=1&__appxPageId=1&'+'mobile='+this.mobile+'&a='+this.a +'&param='+this.param+'&orderNo='+this.orderNo+'&chinfo='+this.chinfo+'&deviceid='+this.deviceid )
								)))
						window.location.href = link
									
							})

								
							

						} else if (/AlipayClient/.test(window.navigator.userAgent)) {

							subOrder({
								a: this.a,
								param: this.param,
								mobile: this.mobile,
								nomobile: '1',
								from_t: '0',
								from_env: '1',
								orderNo: this.orderNo
							}).then((res) => {
				let link = "https://render.alipay.com/p/s/i/?scheme=" +
					encodeURIComponent(
						"alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode="
					) +
					encodeURIComponent(encodeURIComponent('https://render.alipay.com/p/s/i/?scheme=' + encodeURIComponent(
						'alipays://platformapi/startapp?appId=2021004123656452&page=' + encodeURIComponent(
							'pages/newbanner/newbanner?__id__=1&__appxPageId=1&'+'mobile='+this.mobile+'&a='+this.a +'&param='+this.param+'&orderNo='+this.orderNo+'&chinfo='+this.chinfo )
						)))
				window.location.href = link		
							})


						} else {

							// 其他环境
							subOrder({
								a: this.a,
								param: this.param,
								mobile: this.mobile,
								nomobile: '2',
								from_t: '0',
								from_env: '0',
								orderNo: this.orderNo
							}).then((res) => {
	let link = "https://render.alipay.com/p/s/i/?scheme=" +
			encodeURIComponent(
				"alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode="
			) +
		encodeURIComponent(encodeURIComponent('https://render.alipay.com/p/s/i/?scheme=' + encodeURIComponent(
				'alipays://platformapi/startapp?appId=2021004123656452&page=' + encodeURIComponent(
					'pages/newbanner/newbanner?__id__=1&__appxPageId=1&'+'mobile='+this.mobile+'&a='+this.a +'&param='+this.param+'&orderNo='+this.orderNo+'&chinfo='+this.chinfo )
				)))
		window.location.href = link
									
							})



						}
						// #endif
						// #ifdef MP-ALIPAY
						uni.navigateTo({
							url: "/pages/unit/unit"
						})
						my.setStorageSync({
							key: 'phone',
							data: this.mobile
						})
						// #endif

					}
				})




			}

		}
	}
</script>

<style lang="scss">
	
</style>